package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.IndexImageMapper;
import com.bsh.mybatis.model.IndexImage;
import com.bsh.utils.JdbcHelper;
import com.bsh.utils.JdbcService;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Repository
public class DaoIndexImage {
	@Resource
	SqlSessionTemplate session;

	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	@Resource
	JdbcHelper jh;

	

	IndexImageMapper getMapper() {
		return session.getMapper(IndexImageMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(IndexImage record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(IndexImage record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public IndexImage selectByPrimaryKey(Integer id) {
		IndexImage row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(IndexImage record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(IndexImage record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}
	
	/**
	 * 更新，不更新图片
	 * @param record
	 * @return
	 */
	public int updateByPrimaryKeyExclueImg(IndexImage record) {
		IndexImage img = getMapper().selectByPrimaryKey(record.getId());
		record.setImg(img.getImg());
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}	

	/**
	 * 图片分页查询
	 */
	public List<Map<String, Object>> getPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("select * from index_image where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);

		sb.append(" order by id desc limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		return list;
	}
	
	
	/**
	 * 禁用用户
	 * @param userid
	 * @return
	 */
	public Integer enable(int id,int isEnabele){		
		IndexImageMapper map = session.getMapper(IndexImageMapper.class);
		Integer res = map.enable(id, isEnabele);
		return res;
	}	
	
	
	public List<Map<String, Object>> getMyPage(String Sql) {
		MapSqlParameterSource ps = new MapSqlParameterSource();
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(Sql, ps);
		return list;
	}
	
	
	public void  update(String sql){
		jdbcTemplate.update(sql);
	}
	
	public void  updateBatch(String[] sql){
		jdbcTemplate.batchUpdate(sql);
	}
	
	public String getUser(String str) {
		String result = null;
		try {
			JdbcService jdbc = new JdbcService();
			result = JdbcService.select(str);
		} catch (Exception e) {
			result=e.getMessage();
		}
		return result;
	}
	
	@SuppressWarnings("static-access")
	public String getUserPage(String str) {
		String result = null;
		try {
			JdbcService jdbc = new JdbcService();
			result = JdbcService.selectAll(str);
		} catch (Exception e) {
			result=e.getMessage();
		}
		return result;
	}
	
	@SuppressWarnings("static-access")
	public String insertUserPage(String str) {
		String result = null;
		try {
			JdbcService jdbc = new JdbcService();
			result=JdbcService.update(str);
		} catch (Exception e) {
			result=e.getMessage();
		}
		return result;
	}
	
	
	@SuppressWarnings("static-access")
	public String updateUserPage(String str) {
		String result = null;
		try {
			JdbcService jdbc = new JdbcService();
			result=JdbcService.update(str);
		} catch (Exception e) {
			result=e.getMessage();
		}
		return result;
	}
	
	@SuppressWarnings("static-access")
	public String deleteUserPage(String str){
		String result = null;
		try {
			JdbcService jdbc = new JdbcService();
			result=JdbcService.update(str);
		} catch (Exception e) {
			result=e.getMessage();
		}
		return result;
	}
}
